package util.content;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.params.CookiePolicy;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;

public class SpiderContentTxt {
	// 抓取失败后多次重启，每次重启间隔的增量
	final int errorSleepIncrement = 1000;
	HtmlPage spiderPage = null;
	WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
	String title = null;
	String bodyString = null;

	public String getWebSiteBodyString() {
		String bodyString = this.bodyString;
		// System.out.println(bodyString);
		return bodyString;
	}

	public String getTitle() {

		String title = this.title;
		// System.out.println(title);
		return title;
	}

	public void clear() {
		webClient.closeAllWindows();
		webClient.getCookieManager().clearCookies();
	}

	public void SpiderPageContent(String site) {

		// 重连接几次，如果
		for (int reConectTimes = 0; reConectTimes < repeatTimesContent;) {
			try {
				this.spiderPage = webClient.getPage(site);
			} catch (FailingHttpStatusCodeException e) {
				if (e.getStatusCode() == 404 || e.getStatusCode() == 403) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else if (e.getStatusCode() == 400) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else if (reConectTimes == repeatTimesContent) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else
					try {
						Thread.sleep(errorSleepIncrement);
						reConectTimes++;
						continue;
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
			} catch (MalformedURLException e) {
				if (reConectTimes == repeatTimesContent) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else
					try {
						Thread.sleep(errorSleepIncrement);
						reConectTimes++;
						continue;
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
			} catch (IOException e) {
				if (reConectTimes == repeatTimesContent) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else
					try {
						Thread.sleep(errorSleepIncrement);
						reConectTimes++;
						continue;
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
			} catch (Exception e) {
				// 出错链连接错误，连续几次，如果连续连接最多次数title写成0000、content写成0000，否则暂停10秒继续连接
				if (reConectTimes == repeatTimesContent) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else
					try {
						Thread.sleep(errorSleepIncrement);
						reConectTimes++;
						continue;
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
			}
			title = this.spiderPage.getTitleText();
			this.bodyString = spiderPage.getBody().asXml();
			reConectTimes = reConectTimes + repeatTimesContent;
		}
	}

	int repeatTimesContent = 0;

	SpiderContentTxt() {
		String prcString = null;
		// webClient.getCookieManager().setCookiesEnabled(false);
		System.setProperty("apache.commons.httpclient.cookiespec", CookiePolicy.BROWSER_COMPATIBILITY);
		// webClient关闭javascript、设置最长连接时间15秒
		webClient.setJavaScriptEnabled(false);
		webClient.setCssEnabled(false);
		webClient.setTimeout(15000);
		// 设置cookies
		SAXBuilder builder = new SAXBuilder(false);
		List sourceSiteParameterList = null;
		Document doc;
		boolean cookiesTag = false;
		try {
			doc = builder.build("spiderParameter.xml");
			Element parameter = doc.getRootElement();
			sourceSiteParameterList = parameter.getChildren("sourceSite");
			for (Iterator iter = sourceSiteParameterList.iterator(); iter.hasNext();) {
				Element sourceSiteParameter = (Element) iter.next();
				cookiesTag = new Boolean(sourceSiteParameter.getChildTextTrim("cookiesTag")).booleanValue();
				prcString = sourceSiteParameter.getChildTextTrim("proxy");
				if (cookiesTag) {
					List cookiesList = sourceSiteParameter.getChildren("cookies");
					for (Iterator itercookies = cookiesList.iterator(); itercookies.hasNext();) {
						Element cookiesParameter = (Element) itercookies.next();
						String cookiesName = cookiesParameter.getChildTextTrim("cookiesName");
						String cookiesValue = cookiesParameter.getChildTextTrim("cookiesValue");
						String cookiesDomain = cookiesParameter.getChildTextTrim("cookiesDomain");
						webClient.getCookieManager().addCookie(new Cookie(cookiesDomain, cookiesName, cookiesValue));
					}
				}
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 里的代理地址
		Pattern p = Pattern.compile("(.*)(:)(.*)");
		Matcher m = p.matcher(prcString);
		if (m.find()) {
			ProxyConfig prc = new ProxyConfig(m.group(1), new Integer(m.group(3)).intValue());
			webClient.setProxyConfig(prc);
		}
	}

	public void setRepeatTimesContent(int repeatTimesContent) {
		this.repeatTimesContent = repeatTimesContent;
	}
}
